package org.jeecg.modules.golf.document.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.golf.document.entity.PDFDocument;
import org.jeecg.modules.golf.document.enums.PDFDocumentTypeEnum;
import org.jeecg.modules.golf.document.form.PDFDocumentForm;
import org.jeecg.modules.golf.document.service.IPDFDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jiguansheng
 * @date 2021/1/17
 * 档案contoller
 **/
@RestController
@RequestMapping("/document")
public class DocumentController {


    @Autowired
    private IPDFDocumentService pdfDocumentService;

    /**
     * 新增
     * @param
     * @return
     */
    @RequestMapping("save")
    public Result<Boolean> save(@RequestBody PDFDocumentForm dto){
        return Result.OK(pdfDocumentService.save(convertAddPdf(dto)));
    }

    /**
     * 新增
     * @param
     * @return
     */
    @RequestMapping("del")
    public Result<Boolean> del(@RequestBody PDFDocumentForm dto){
        return Result.OK(pdfDocumentService.removeById(dto.getId()));
    }

    private PDFDocument convertAddPdf(PDFDocumentForm pdfDTO){
        PDFDocument pdfDocument = new PDFDocument();
        pdfDocument.setId(pdfDTO.getId());
        pdfDocument.setFile(pdfDTO.getFile());
        pdfDocument.setName(pdfDTO.getName());
        pdfDocument.setObjectId(pdfDTO.getUserId());
        pdfDocument.setObjectType(PDFDocumentTypeEnum.USER.getCode());
        return pdfDocument;
    }

    /**
     * 录入档案
     * 需要录入 objectId 和 objectType 。查询对应所有的档案
     * @param pdfDocument
     * @returnR
     */
    @RequestMapping("list")
    public Result<List<PDFDocument>> list(PDFDocument pdfDocument){
        QueryWrapper<PDFDocument> queryWrapper = new QueryWrapper<>(pdfDocument);
        return Result.OK(pdfDocumentService.list(queryWrapper));
    }

}
